home *** CD-ROM | disk | FTP | other *** search
/ Aminet 1 (Walnut Creek) / Aminet - June 1993 [Walnut Creek].iso / aminet / util / gnu / fileutils_3_3.lha / fileutils-3.3 / lib / userspec.c < prev    next >
C/C++ Source or Header  |  1992-08-01  |  4KB  |  179 lines

  1. /* userspec.c -- Parse a user and group string.
  2.    Copyright (C) 1989, 1990, 1991, 1992 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* Written by David MacKenzie <djm@gnu.ai.mit.edu>.  */
  19.  
  20. #include <stdio.h>
  21. #include <sys/types.h>
  22. #include <pwd.h>
  23. #include <grp.h>
  24.  
  25. #if defined(USG) || defined(STDC_HEADERS)
  26. #include <string.h>
  27. #define index strchr
  28. #else
  29. #include <strings.h>
  30. #endif
  31.  
  32. #ifdef STDC_HEADERS
  33. #include <stdlib.h>
  34. #else
  35. char *malloc ();
  36. #endif
  37.  
  38. #ifdef HAVE_UNISTD_H
  39. #include <unistd.h>
  40. #endif
  41.  
  42. #ifndef _POSIX_VERSION
  43. struct passwd *getpwnam ();
  44. struct group *getgrnam ();
  45. struct group *getgrgid ();
  46. #endif
  47.  
  48. #ifdef _POSIX_SOURCE
  49. #define endpwent()
  50. #define endgrent()
  51. #endif
  52.  
  53. #define isdigit(c) ((c) >= '0' && (c) <= '9')
  54.  
  55. char *strdup ();
  56. static int isnumber ();
  57.  
  58. /* Extract from NAME, which has the form "[user][:.][group]",
  59.    a USERNAME, UID U, GROUPNAME, and GID G.
  60.    Either user or group, or both, must be present.
  61.    If the group is omitted but the ":" or "." separator is given,
  62.    use the given user's login group.
  63.  
  64.    USERNAME and GROUPNAME will be in newly malloc'd memory.
  65.    Either one might be NULL instead, indicating that it was not
  66.    given and the corresponding numeric ID was left unchanged.
  67.    Might write NULs into NAME.
  68.  
  69.    Return NULL if successful, a static error message string if not.  */
  70.  
  71. char *
  72. parse_user_spec (name, uid, gid, username, groupname)
  73.      char *name;
  74.      uid_t *uid;
  75.      gid_t *gid;
  76.      char **username, **groupname;
  77. {
  78.   static char *tired = "virtual memory exhausted";
  79.   struct passwd *pwd;
  80.   struct group *grp;
  81.   char *cp;
  82.   int use_login_group = 0;
  83.  
  84.   *username = *groupname = NULL;
  85.  
  86.   /* Check whether a group is given.  */
  87.   cp = index (name, ':');
  88.   if (cp == NULL)
  89.     cp = index (name, '.');
  90.   if (cp != NULL)
  91.     {
  92.       *cp++ = '\0';
  93.       if (*cp == '\0')
  94.     {
  95.       if (cp == name + 1)
  96.         /* Neither user nor group given, just "." or ":".  */
  97.         return "can not omit both user and group";
  98.       else
  99.         /* "user.".  */
  100.         use_login_group = 1;
  101.     }
  102.       else
  103.     {
  104.       /* Explicit group.  */
  105.       *groupname = strdup (cp);
  106.       if (*groupname == NULL)
  107.         return tired;
  108.       grp = getgrnam (cp);
  109.       if (grp == NULL)
  110.         {
  111.           if (!isnumber (cp))
  112.         return "invalid group";
  113.           *gid = atoi (cp);
  114.         }
  115.       else
  116.         *gid = grp->gr_gid;
  117.       endgrent ();        /* Save a file descriptor.  */
  118.     }
  119.     }
  120.  
  121.   /* Parse the user name, now that any group has been removed.  */
  122.  
  123.   if (name[0] == '\0')
  124.     /* No user name was given, just a group.  */
  125.     return NULL;
  126.  
  127.   *username = strdup (name);
  128.   if (*username == NULL)
  129.     return tired;
  130.  
  131.   pwd = getpwnam (name);
  132.   if (pwd == NULL)
  133.     {
  134.       if (!isnumber (name))
  135.     return "invalid user";
  136.       if (use_login_group)
  137.     return "cannot get the login group of a numeric UID";
  138.       *uid = atoi (name);
  139.     }
  140.   else
  141.     {
  142.       *uid = pwd->pw_uid;
  143.       if (use_login_group)
  144.     {
  145.       *gid = pwd->pw_gid;
  146.       grp = getgrgid (pwd->pw_gid);
  147.       if (grp == NULL)
  148.         {
  149.           *groupname = malloc (15);
  150.           if (*groupname == NULL)
  151.         return tired;
  152.           sprintf (*groupname, "%u", pwd->pw_gid);
  153.         }
  154.       else
  155.         {
  156.           *groupname = strdup (grp->gr_name);
  157.           if (*groupname == NULL)
  158.         return tired;
  159.         }
  160.       endgrent ();
  161.     }
  162.     }
  163.   endpwent ();
  164.   return NULL;
  165. }
  166.  
  167. /* Return nonzero if STR represents an unsigned decimal integer,
  168.    otherwise return 0. */
  169.  
  170. static int
  171. isnumber (str)
  172.      char *str;
  173. {
  174.   for (; *str; str++)
  175.     if (!isdigit (*str))
  176.       return 0;
  177.   return 1;
  178. }
  179.